package vip.liux.front.infrastructure.utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AmbientDataContext {
    private final static Map<String, ThreadLocal<Object>> AsyncLocalDictionary = new ConcurrentHashMap<>();

    public void set(String key, Object value) {
        ThreadLocal<Object> threadLocal = AsyncLocalDictionary.get(key);
        if (threadLocal == null) {
            threadLocal = new ThreadLocal<>();
            AsyncLocalDictionary.put(key, threadLocal);
        }
        threadLocal.set(value);
    }

    public Object get(String key) {
        ThreadLocal<Object> threadLocal = AsyncLocalDictionary.get(key);
        if (threadLocal == null) {
            return null;
        }
        return threadLocal.get();
    }
}